package com.sau.pixelcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sau.pixelcart.common.result.PageResult;
import com.sau.pixelcart.domain.dto.InventoryPageDTO;
import com.sau.pixelcart.domain.entity.InventoryAdjustment;
import com.sau.pixelcart.domain.entity.Product;
import com.sau.pixelcart.domain.entity.User;
import com.sau.pixelcart.domain.vo.InventoryAdjustmentVO;
import com.sau.pixelcart.mapper.InventoryAdjustmentMapper;
import com.sau.pixelcart.mapper.ProductMapper;
import com.sau.pixelcart.mapper.UserMapper;
import com.sau.pixelcart.service.IInventoryAdjustmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 库存操作审计表 服务实现类
 * </p>
 *
 * @author Inton
 * @since 2025-03-08
 */
@Service
public class InventoryAdjustmentServiceImpl extends ServiceImpl<InventoryAdjustmentMapper, InventoryAdjustment> implements IInventoryAdjustmentService {

    @Autowired
    private  InventoryAdjustmentMapper inventoryAdjustmentMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;


    /**
     * 库存操作记录查询
     * @param inventoryPageDTO
     * @return
     */
    @Override
    public PageResult<InventoryAdjustmentVO> pageQuery(InventoryPageDTO inventoryPageDTO) {
        Page<InventoryAdjustment> page = new Page<>(inventoryPageDTO.getPage(),inventoryPageDTO.getPageSize());

        LambdaQueryWrapper<InventoryAdjustment> inventoryAdjustmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(inventoryPageDTO.getStatus() != null && !inventoryPageDTO.getStatus().equals("")){
            inventoryAdjustmentLambdaQueryWrapper.eq(InventoryAdjustment::getReason,inventoryPageDTO.getStatus());
        }

        inventoryAdjustmentLambdaQueryWrapper.orderByDesc(InventoryAdjustment::getAdjustmentTime);

        Page<InventoryAdjustment> inventoryAdjustmentPage = inventoryAdjustmentMapper.selectPage(page, inventoryAdjustmentLambdaQueryWrapper);
        List<InventoryAdjustmentVO> invnetoryAdjustmentVOList = inventoryAdjustmentPage.getRecords().stream().map(inventoryAdjustment -> {
            InventoryAdjustmentVO inventoryAdjustmentVO = new InventoryAdjustmentVO();
            BeanUtils.copyProperties(inventoryAdjustment, inventoryAdjustmentVO);
            inventoryAdjustmentVO.setChangeStock(inventoryAdjustment.getNewStock() - inventoryAdjustment.getOldStock());
            User user = userMapper.selectById(inventoryAdjustment.getOperatorId());
            inventoryAdjustmentVO.setOperatorName(user.getUsername());

            Product product = productMapper.selectById(inventoryAdjustment.getProductId());
            inventoryAdjustmentVO.setProductCode(product.getCode());
            inventoryAdjustmentVO.setProductName(product.getName());

            return inventoryAdjustmentVO;
        }).collect(Collectors.toList());

        PageResult<InventoryAdjustmentVO> pageResult = new PageResult<>();
        BeanUtils.copyProperties(inventoryAdjustmentPage,pageResult);
        pageResult.setRecords(invnetoryAdjustmentVOList);

        return pageResult;
    }
}
